package com.carrental.vehicle.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.common.result.Result;
import com.carrental.vehicle.dto.VehicleDTO;
import com.carrental.vehicle.entity.Vehicle;
import com.carrental.vehicle.service.VehicleService;
import com.carrental.vehicle.vo.VehicleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆控制器
 */
@Slf4j
@Api(tags = "车辆管理")
@RestController
@RequestMapping("/vehicles")
@RequiredArgsConstructor
public class VehicleController {

    private final VehicleService vehicleService;

    @ApiOperation("根据ID获取车辆详情")
    @GetMapping("/{id}")
    public Result<VehicleVO> getVehicleById(
            @ApiParam("车辆ID") @PathVariable Long id) {
        return Result.success(vehicleService.getVehicleById(id));
    }

    @ApiOperation("保存或更新车辆信息")
    @PostMapping("/save")
    public Result<Boolean> saveOrUpdateVehicle(
            @ApiParam("车辆信息") @Validated @RequestBody VehicleDTO vehicleDTO) {
        return Result.success(vehicleService.saveOrUpdateVehicle(vehicleDTO));
    }

    @ApiOperation("根据ID删除车辆")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteVehicleById(
            @ApiParam("车辆ID") @PathVariable Long id) {
        return Result.success(vehicleService.deleteVehicleById(id));
    }

    @ApiOperation("分页查询车辆")
    @GetMapping("/page")
    public Result<Page<VehicleVO>> pageVehicle(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("品牌ID") @RequestParam(required = false) Long brandId,
            @ApiParam("系列ID") @RequestParam(required = false) Long seriesId,
            @ApiParam("车辆类型") @RequestParam(required = false) Integer type,
            @ApiParam("车辆状态") @RequestParam(required = false) Integer status,
            @ApiParam("门店ID") @RequestParam(required = false) Long storeId,
            @ApiParam("车辆名称") @RequestParam(required = false) String name,
            @ApiParam("车牌号") @RequestParam(required = false) String licensePlate) {
        Page<Vehicle> page = new Page<>(current, size);
        Vehicle vehicle = new Vehicle();
        vehicle.setBrandId(brandId);
        vehicle.setSeriesId(seriesId);
        vehicle.setType(type);
        vehicle.setStatus(status);
        vehicle.setStoreId(storeId);
        vehicle.setName(name);
        vehicle.setLicensePlate(licensePlate);
        return Result.success(vehicleService.pageVehicle(page, vehicle));
    }

    @ApiOperation("根据车辆类型查询车辆列表")
    @GetMapping("/list/type/{type}")
    public Result<List<VehicleVO>> listVehicleByType(
            @ApiParam("车辆类型") @PathVariable Integer type) {
        return Result.success(vehicleService.listVehicleByType(type));
    }

    @ApiOperation("根据门店ID查询车辆列表")
    @GetMapping("/list/store/{storeId}")
    public Result<List<VehicleVO>> listVehicleByStore(
            @ApiParam("门店ID") @PathVariable Long storeId) {
        return Result.success(vehicleService.listVehicleByStore(storeId));
    }

    @ApiOperation("根据品牌ID查询车辆列表")
    @GetMapping("/list/brand/{brandId}")
    public Result<List<VehicleVO>> listVehicleByBrand(
            @ApiParam("品牌ID") @PathVariable Long brandId) {
        return Result.success(vehicleService.listVehicleByBrand(brandId));
    }

    @ApiOperation("更新车辆状态")
    @PutMapping("/{id}/status/{status}")
    public Result<Boolean> updateVehicleStatus(
            @ApiParam("车辆ID") @PathVariable Long id,
            @ApiParam("车辆状态") @PathVariable Integer status) {
        return Result.success(vehicleService.updateVehicleStatus(id, status));
    }

    @ApiOperation("获取推荐列表")
    @GetMapping("/recommend")
    public Result<List<VehicleVO>> ListRecommend( @RequestParam(defaultValue = "10") int limit,
                                                  @RequestParam(defaultValue = "") String tags) {
        //todo
        return Result.success(null);
    }

    @ApiOperation("获取热门车辆")
    @GetMapping("/popular")
    public Result<List<VehicleDTO>> getPopularVehicles(@RequestParam(defaultValue = "6") Integer limit) {
        log.info("获取热门车型列表，限制数量: {}", limit);
        List<VehicleDTO> popularVehicles = vehicleService.getPopularVehicles(limit);
        return Result.success(popularVehicles);
    }

    @ApiOperation("获取车辆详情")
    @GetMapping("/detail/{id}")
    public Result<VehicleDTO> getVehicleDetail(@PathVariable("id") Long id) {
        log.info("获取车辆详情，ID：{}", id);
        return Result.success(vehicleService.getVehicleDetail(id));
    }

    @ApiOperation(value = "更新车辆租赁次数", notes = "内部接口，仅供微服务间调用")
    @PostMapping("/internal/updateRentalCount")
    public Result<Boolean> updateRentalCount(@RequestParam("vehicleId") Long vehicleId) {
        log.info("更新车辆租赁次数，车辆ID：{}", vehicleId);
        return Result.success(vehicleService.updateRentalCount(vehicleId));
    }
} 